package sk.tramtatiri;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.tramtatiri.data.dao.interfaces.FoodDao;
import sk.tramtatiri.data.dao.interfaces.FriendshipDao;
import sk.tramtatiri.data.dao.interfaces.InOutHelperDao;
import sk.tramtatiri.data.dao.interfaces.RecipeDao;
import sk.tramtatiri.data.dao.interfaces.SportDao;
import sk.tramtatiri.data.dao.interfaces.UserDao;
import sk.tramtatiri.data.dao.interfaces.UserFoodDao;
import sk.tramtatiri.data.dao.interfaces.UserInfoDao;
import sk.tramtatiri.data.dao.interfaces.UserRecipeDao;
import sk.tramtatiri.data.dao.interfaces.UserSportDao;
import sk.tramtatiri.data.dataobjects.Food;
import sk.tramtatiri.data.dataobjects.Friendship;
import sk.tramtatiri.data.dataobjects.InOutHelper;
import sk.tramtatiri.data.dataobjects.Recipe;
import sk.tramtatiri.data.dataobjects.Sport;
import sk.tramtatiri.data.dataobjects.User;
import sk.tramtatiri.data.dataobjects.UserFood;
import sk.tramtatiri.data.dataobjects.UserInfo;
import sk.tramtatiri.data.dataobjects.UserRecipe;
import sk.tramtatiri.data.dataobjects.UserSport;
import sk.tramtatiri.pages.users.UserHomePage;

public class DataInit extends WebPage {

	@SpringBean
	private UserDao userDao;

	@SpringBean
	private UserInfoDao userInfoDao;

	@SpringBean
	private SportDao sportDao;

	@SpringBean
	private FoodDao foodDao;

	@SpringBean
	private RecipeDao recipeDao;

	@SpringBean
	private FriendshipDao friendshipDao;

	@SpringBean
	private InOutHelperDao inOutHelperDao;

	@SpringBean
	private UserFoodDao userFoodDao;

	@SpringBean
	private UserRecipeDao userRecipeDao;

	@SpringBean
	private UserSportDao userSportDao;

	private List<User> users = new ArrayList<User>();
	private List<Sport> sports = new ArrayList<Sport>();
	private List<Recipe> recipes = new ArrayList<Recipe>();
	private List<Food> foods = new ArrayList<Food>();

	private static int[][] friendsIndexes = new int[][] { { 1, 3, 5 },
			{ 0, 2, 6 }, { 1, 3, 8 }, { 0, 2, 5 }, { 5, 6, 7 }, { 0, 3, 4 },
			{ 1, 4, 9 }, { 4, 8, 9 }, { 2, 7 }, { 6, 7 } };
	private static int[] heights = new int[] { 188, 174, 159, 177, 180, 161,
			190, 175, 168, 176 };
	private static String[] userNames = new String[] { "Frantisek Stromokocur",
			"Jan Belavy", "Olivia Kosicka", "Albert Maly", "Fedor Vysoky",
			"Viera Novakova", "Jan Novak", "Vincent Lenivy", "Maria Matuskova",
			"Peter Petrovsky" };
	private static String[] userEmails = new String[] {
			"f.stromokocur@gmail.com", "belavyjan@gmail.com",
			"okosicka@gmail.com", "maly.ali@gmail.com", "fedorv@gmail.com",
			"vnovakova@gmail.com", "jan.novak@gmail.com", "vinco@gmail.com",
			"matuskovam@gmail.com", "peter.peter@gmail.com" };
	private static String[] sex = new String[] { "Muz", "Muz", "Zena", "Muz",
			"Muz", "Zena", "Muz", "Muz", "Zena", "Muz" };
	private static int[][] weights = new int[][] {
			{ 100, 100, 100, 99, 98, 98, 97, 97, 97, 96 },
			{ 80, 79, 79, 79, 78, 78, 77, 77, 76, 76 },
			{ 55, 55, 55, 55, 54, 54, 54, 53, 54, 53 },
			{ 100, 100, 100, 99, 98, 98, 97, 97, 97, 96 },
			{ 80, 79, 79, 79, 78, 78, 77, 77, 76, 76 },
			{ 55, 55, 55, 55, 54, 54, 54, 53, 54, 53 },
			{ 100, 100, 100, 99, 98, 98, 97, 97, 97, 96 },
			{ 80, 79, 79, 79, 78, 78, 77, 77, 76, 76 },
			{ 55, 55, 55, 55, 54, 54, 54, 53, 54, 53 },
			{ 80, 79, 79, 79, 78, 78, 77, 77, 76, 76 } };
	private static int[][] waists = new int[][] {
			{ 98, 98, 98, 98, 98, 97, 97, 97, 96, 96 },
			{ 80, 79, 79, 78, 78, 77, 78, 78, 77, 77 },
			{ 70, 69, 69, 68, 68, 67, 67, 67, 66, 66 },
			{ 98, 98, 98, 98, 98, 97, 97, 97, 96, 96 },
			{ 80, 79, 79, 78, 78, 77, 78, 78, 77, 77 },
			{ 70, 69, 69, 68, 68, 67, 67, 67, 66, 66 },
			{ 98, 98, 98, 98, 98, 97, 97, 97, 96, 96 },
			{ 80, 79, 79, 78, 78, 77, 78, 78, 77, 77 },
			{ 70, 69, 69, 68, 68, 67, 67, 67, 66, 66 },
			{ 80, 79, 79, 78, 78, 77, 78, 78, 77, 77 } };
	private static String[] dates = new String[] { "2011-11-29", "2011-11-30",
			"2011-12-01", "2011-12-02", "2011-12-03", "2011-12-04",
			"2011-12-05", "2011-12-06", "2011-12-07", "2011-12-08" };
	private static String[] birthDates = new String[] { "1988-11-28",
			"1980-08-29", "1990-12-01", "1987-04-22", "1984-06-17",
			"1984-02-15", "1985-10-18", "1982-10-21", "1981-01-15",
			"1979-03-08" };

	private static String[] sportNames = new String[] { "Zumba", "Aerobik",
			"Aqua-Aerobik", "Klasický tanec", "Moderný tanec", "Country tanec",
			"Brušný tanec", "Street dance", "Tenis", "Squash", "Bedminton",
			"Basketbal", "Volejbal", "Hádzaná", "Chôdza po rovine",
			"Chôdza do kopca", "Beh", "Plávanie", "Kolieskové korčule",
			"Jazda na bicykli", "Lyžovanie", "Korčuľovanie", "Tai-či", "Judo",
			"Karate", "Kick-box", "Tae-kwon-do" };
	private static String[] sportCategories = new String[] { "Aerobik",
			"Aerobik", "Aerobik", "Tanec", "Tanec", "Tanec", "Tanec", "Tanec",
			"Kolektívne športy", "Kolektívne športy", "Kolektívne športy",
			"Kolektívne športy", "Kolektívne športy", "Kolektívne športy",
			"Individuálne športy", "Individuálne športy",
			"Individuálne športy", "Individuálne športy",
			"Individuálne športy", "Individuálne športy",
			"Individuálne športy", "Individuálne športy", "Bojové športy",
			"Bojové športy", "Bojové športy", "Bojové športy", "Bojové športy" };
	private static int[] sportCalories = new int[] { 900, 600, 600, 300, 300,
			300, 420, 500, 550, 800, 490, 550, 300, 800, 420, 900, 740, 750,
			600, 900, 600, 480, 270, 850, 850, 850, 850 };

	private static String[] recipeNames = { "Segedínsky guláš",
			"Slivkovo pudingový mrežovník", "Cuketová pizza",
			"Zemiakové fašírky", "Zeler plnený mäsovou plnkou",
			"Vínne klobásky", "Zapekané kuracie krídla", "Zbojnícke toasty",
			"Zapekané rezne", "Zapekaná zemiaková placka" };
	private static String[] recipeIngredients = { "Jablko", "Bryndza",
			"Zemiaky varené", "Ryža", "Paradajky" };
	// private static String[] dummyDescriptions = {"Padlo na hlavu Newtona.",
	// "Lyze", "Hokej", "Inline", "Beh", "Posilka", "Tenis", "Florbal",
	// "Hokejbal", "Plavanie"};
	private static int[] recipeCalories = { 2674, 3245, 2521, 2786, 2558, 2784,
			2123, 2215, 2444, 2235 };
	private static int[] recipeFats = { 18, 12, 13, 26, 22, 34, 20, 18, 32, 19 };
	private static int[] recipeProteins = { 35, 47, 28, 42, 38, 62, 52, 39, 72,
			39 };
	private static int[] recipeSaccharides = { 14, 69, 15, 7, 12, 23, 16, 11,
			9, 21 };
	private static long[] recipeCreatorIDs = { 1, 4, 5, 2, 1, 3, 4, 2, 5, 1 };
	private static int[] recipeRatings = { 4, 8, 10, 5, 4, 7, 9, 4, 4, 2 };
	private static int[] recipeDifficulties = { 1, 4, 5, 2, 1, 3, 4, 2, 5, 1 };

	private static String[] foodNames = { "Jablko", "Kuracie mäso", "Zemiaky",
			"Fazuľa", "Bryndza", "Olivový olej", "Ryža", "Mlieko polotučné",
			"Kečup", "Paradajka" };
	private static String[] foodDescriptions = {
			"Jablko je druh ovocia, je to plod jablone. Jablko je malvica.",
			"Kuracie mäso je druh mäsa, ktorý pochádza z kuriat. Jedná sa o celosvetovo nejviac rozšírený druh mäsa, ktorý je konzumovýn.",
			"Ľuľok zemiakový (Solanum tuberosum), v bežnej reči len zemiak, je viacročná hľuznatá plodina z čeľade ľuľkovité (Solanaceae). Zemiaky patria medzi najvýznamnejšie poľnohospodárske plodiny.",
			"Fazuľa (Phaseolus) je rod strukovín. Plody fazule sú lusky, ktoré obsahujú niekoľko semiačok, označovaných jako fazuľe. Naše najznámějšie fazule sú hnedé, niektoré sú ale i zelené a žlté. Vo fazuľiach je mnoho vitamínov.",
			"Bryndza (z rum. brânză), je mäkký, miesený a solený ovčí syr vyrábaný z hrudkového ovčieho syra. Vyrába sa na Slovensku, v Poľsku, Rumunsku, Ukrajine a v Bulharsku. Konzumujú ju aj v Rakúsku a Bavorsku (Brimsen, Liptauer Käse), Česku a pod.",
			"Olivový olej je olej získaný z oliv (Olea europaea) lisovaním či inými mechanickými postupmi. Používa sa predovšetkým pri výrobe pokrmov, v kozmetickom a farmaceutickom priemysle. Olivový olej je typickou součásťou stredomorskej kuchyne.",
			"Ryža sú plody druhov ryža siata (Oryza sativa) alebo Oryza glaberrima z rodu ryža. Pochádzajú pôvodne z tropickej a subtropickej južnej a juhovýchodnej Ázie a Afriky.",
			"Mlieko je produkt mliečných žliaz samic savcov. Mlieko je základným zdrojom výživy hlavne pre mláďata, ktoré z tzv. „mleziva“ získávajú potrebné protilátky a vitamíny pre upevnenie svojej imunity.",
			"Kečup je druh hustej zeleninovej omáčky. Jeho otcom je Henry Heinz (1844-1919). Je vyrobený z paradajkového pretlaku, korenia a ďalších prísad, preto má červené zafarbenie. Zvyčajne sa používa na ochutenie jedál.",
			"Rajčiak jedlý (Solanum lycopersicum; neodborne, terminologické synonymá: rajčiak, rajčina, paradajka) je druh rastliny z čeľade ľuľkovité (Solanaceae), čím je príbuzná s takými rastlinami, ako je zemiak, mandragora alebo petúnia." };
	private static int[] foodCalories = { 175, 550, 327, 1179, 1328, 3748, 485,
			194, 500, 65 };
	private static int[] foodFats = { 0, 2, 0, 1, 25, 99, 0, 1, 1, 0 };
	private static int[] foodProteins = { 0, 30, 2, 22, 20, 0, 2, 3, 1, 0 };
	private static int[] foodSaccharides = { 12, 0, 20, 59, 1, 0, 26, 4, 28, 4 };
	private static long[] foodCreatorIDs = { 1, 4, 5, 2, 1, 3, 4, 2, 5, 1 };
	private static int[] foodRatings = { 4, 8, 10, 5, 4, 7, 9, 4, 4, 2 };
	private static String[] foodCategoryes = { "Jablko", "Lyze", "Hokej",
			"Inline", "Beh", "Posilka", "Tenis", "Florbal", "Hokejbal",
			"Plavanie" };
	private static String[] foodSubcategoryes = { "Jablko", "Lyze", "Hokej",
			"Inline", "Beh", "Posilka", "Tenis", "Florbal", "Hokejbal",
			"Plavanie" };

	public DataInit() {
		aInit();
		add(new Link<Void>("backToHome") {
			@Override
			public void onClick() {
				setResponsePage(UserHomePage.class);
			}
		});
	}

	private void aInit() {
		User user = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		for (int i = 0; i < 10; i++) {
			user = new User();
			try {
				user.setBirthDate(sdf.parse(birthDates[i]));
			} catch (ParseException e1) {
			}
			user.setEmail(userEmails[i]);
			user.setHeight(heights[i]);
			user.setNickName(userNames[i]);
			user.setPassword(((WicketApplication) getApplication())
					.hashPassword("String123"));
			user.setSex(sex[i]);
			userDao.save(user);

			UserInfo ui = null;
			for (int j = 0; j < 10; j++) {
				ui = new UserInfo();
				try {
					ui.setDate(sdf.parse(dates[j]));
				} catch (ParseException e) {
				}
				ui.setUser(user);
				ui.setUser_id(user.getId());
				ui.setWaist(waists[i][j]);
				ui.setWeight(weights[i][j]);
				userInfoDao.save(ui);
				user.getUserInfos().add(ui);
			}
			users.add(user);

		}

		Sport sport = null;
		for (int k = 0; k < sportNames.length; k++) {
			sport = new Sport();
			sport.setCalories(sportCalories[k]);
			sport.setCategory(sportCategories[k]);
			sport.setDifficulty(0);
			sport.setName(sportNames[k]);
			sport.setRating(0);
			sportDao.save(sport);

			sports.add(sport);
		}

		for (int l = 0; l < 10; l++) {
			Recipe recipe = new Recipe();
			recipe.setPortion(4);
			recipe.setName(recipeNames[l]);
			recipe.setIngredients(recipeIngredients);
			recipe.setDescription("Je to chutné jedlo");
			recipe.setRating(recipeRatings[l]);
			recipe.setCalories(recipeCalories[l]);
			recipe.setDifficulty(recipeDifficulties[l]);
			recipe.setCreatorID(recipeCreatorIDs[l]);
			recipe.setFats(recipeFats[l]);
			recipe.setProteins(recipeProteins[l]);
			recipe.setSaccharides(recipeSaccharides[l]);
			recipe.setAllValues(true);
			recipe.setCreatorID(1);
			recipe.setWeights(recipeCalories);
			recipeDao.save(recipe);

			recipes.add(recipe);
		}

		for (int m = 0; m < 10; m++) {
			Food food = new Food();
			food.setName(foodNames[m]);
			food.setDescription(foodDescriptions[m]);
			food.setRating(foodRatings[m]);
			food.setCalories(foodCalories[m]);
			food.setCategory(foodCategoryes[m]);
			food.setSubcategory(foodSubcategoryes[m]);
			food.setCreatorID(foodCreatorIDs[m]);
			food.setFats(foodFats[m]);
			food.setProteins(foodProteins[m]);
			food.setSaccharides(foodSaccharides[m]);
			foodDao.save(food);

			foods.add(food);
		}

		int calIn = 0;
		int calOut = 0;
		double pom = 0;
		int duration = 0;
		int totalCalories = 0;
		int inKJ = 0;
		Random rand = new Random();
		Sport s = null;
		User u = null;
		UserSport us = null;
		Food f = null;
		Recipe rec = null;
		UserRecipe ur = null;
		UserFood uf = null;
		int sportIndex;
		int foodIndex;
		int recipeIndex;
		Friendship friendship = null;
		InOutHelper todayInOut;
		int friendIndex;
		for (int o = 0; o < 10; o++) { // 10 userov
			u = users.get(o);
			for (int p = 0; p < 10; p++) { // 10 dni
				calOut = 0;
				calIn = 0;
				for (int q = 0; q < 2; q++) { // 2 sporty denne
					sportIndex = rand.nextInt(27);
					s = sports.get(sportIndex);
					us = new UserSport();
					try {
						us.setDate(sdf.parse(dates[p]));
					} catch (ParseException e) {
					}
					duration = rand.nextInt(31) + 30;
					pom = (duration) / 60.0;
					totalCalories = (int) Math.round(pom * s.getCalories());
					inKJ = Math.round((float) (totalCalories * 4.19));
					us.setDuration(duration);
					us.setSport(s);
					us.setSport_id(s.getId());
					us.setTotalCalories(inKJ);
					us.setUser(u);
					us.setUser_id(u.getId());

					userSportDao.save(us);

					calOut = calOut + inKJ;
					u.getSports().add(us);
					s.getUsers().add(us);
				}

				// 1 hlavne jedlo denne
				recipeIndex = rand.nextInt(10);
				rec = recipes.get(recipeIndex);
				ur = new UserRecipe();
				try {
					ur.setDate(sdf.parse(dates[p]));
				} catch (ParseException e) {
				}
				ur.setDayTime(0);
				ur.setRecipe(rec);
				ur.setRecipeId(rec.getId());
				ur.setTotalCalories(rec.getCalories());
				ur.setUser(u);
				ur.setUserId(u.getId());

				userRecipeDao.save(ur);

				u.getRecipes().add(ur);
				rec.getUsers().add(ur);

				calIn = calIn + rec.getCalories();

				for (int t = 0; t < 3; t++) { // 3 potraviny denne
					foodIndex = rand.nextInt(10);
					f = foods.get(foodIndex);

					uf = new UserFood();
					try {
						uf.setDate(sdf.parse(dates[p]));
					} catch (ParseException e) {
					}
					uf.setFood(f);
					uf.setFoodId(f.getId());
					uf.setQuantity(100);
					uf.setTotalCalories(f.getCalories());
					uf.setUser(u);
					uf.setUserId(u.getId());

					userFoodDao.save(uf);

					u.getFoods().add(uf);
					f.getUsers().add(uf);

					calIn = calIn + f.getCalories();
				}

				todayInOut = new InOutHelper();
				try {
					todayInOut.setDate(sdf.parse(dates[p]));
				} catch (ParseException e) {
				}
				todayInOut.setCaloriesIn(calIn);
				todayInOut.setCaloriesOut(calOut);
				todayInOut.setUsrid(u.getId());
				inOutHelperDao.save(todayInOut);
			}
			for (int a = 0; a < 3; a++) {
				if (o < 8) {
					friendIndex = friendsIndexes[o][a];
					friendship = new Friendship();
					friendship.setUser(u);
					friendship.setUserId(u.getId());
					friendship.setFriend_id(friendIndex + 1);
					friendship.setFriend(users.get(friendIndex));
					friendship.setSharedStats(1);

					friendshipDao.save(friendship);
					u.getFriendships().add(friendship);
				}
			}

		}
	}
}
